package com.example.conference.controller;

import com.example.conference.domain.User;
import com.example.conference.domain.dto.PageParam;
import com.example.conference.mapper.UserMapper;
import com.example.conference.service.UserServiceImpl;
import com.example.conference.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@CrossOrigin(originPatterns = "*",allowCredentials = "true")
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserServiceImpl userService;
    @Autowired
    UserMapper userMapper;

    @RequestMapping("/add")
    public Result addUser(@RequestBody User u) {
        return userService.addUser(u);
    }
    @RequestMapping("/update")
    public Result updateUser(@RequestBody User u) {
        return userService.updateUser(u);
    }
    @RequestMapping("/query")
    public Result queryUser(@RequestBody PageParam page){ return userService.queryUser(page);}
    @RequestMapping("/delete/{useId}")
    public Result deleteUser(@PathVariable Integer useId){ return userService.deleteUser(useId);}
    @RequestMapping("/queryName/{name}")
    public Result queryUserByname(@RequestBody PageParam page,@PathVariable String name){ return userService.queryUserByName(page,name);}

    @RequestMapping("/test/{id}")
    public Result querytest(@PathVariable Integer id) {
        return Result.success().data("items", userMapper.queryByIdToVo(id));
    }
    //查询学生信息
    @RequestMapping("/queryStudent")
    public Result queryStudent(@RequestBody PageParam page){ return userService.queryUserByRole(page,3);}
    //根据系别查询学生信息
    @RequestMapping("/queryStudentByDept/{deptId}")
    public List<User> queryStudentByDept(@PathVariable Integer deptId){ return userService.queryUserByRoleAndDept(3,deptId);}
    //查询教师信息
    @RequestMapping("/queryTeacher")
    public Result queryTeacher(@RequestBody PageParam page){ return userService.queryUserByRole(page,2);}
    //根据系别查询教师信息
    @RequestMapping("/queryTeacherByDept/{deptId}")
    public List<User> queryTeacherByDept(@PathVariable Integer deptId){ return userService.queryUserByRoleAndDept(2,deptId);}


    @ExceptionHandler
    public Result handlerException(Exception e) {
        e.printStackTrace();
        return Result.error(e.getMessage());
    }


}
